package com.szxy.controller.system;

import com.szxy.aspectj.annotation.Log;
import com.szxy.aspectj.enums.BusinessType;
import com.szxy.aspectj.enums.OperatorType;
import com.szxy.dto.DictDataDto;
import com.szxy.service.DictDataService;
import com.szxy.utils.ShiroSecurityUtils;
import com.szxy.vo.AjaxResult;
import com.szxy.vo.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * @Author: zwz
 * @Date:2020/11/5 22:31
 * @Description: 数据字典
 **/
@RestController
@RequestMapping("system/dict/data")
public class DictDataController {

    @Autowired
    private DictDataService dictDataService;

    /*** 分页查询 */
    @GetMapping("listForPage")
    public AjaxResult listForPage(DictDataDto DictDataDto) {
        DataGridView gridView = this.dictDataService.listPage(DictDataDto);
        return AjaxResult.success("查询成功", gridView.getData(), gridView.getTotal());
    }

    /*** 添加 */
    @PostMapping("addDictData")
    @Log(title = "添加字典数据", businessType = BusinessType.INSERT, operatorType = OperatorType.OTHER)
    public AjaxResult addDictData(@Validated DictDataDto DictDataDto) {
        DictDataDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.dictDataService.insert(DictDataDto));
    }

    /*** 修改 */
    @PutMapping("updateDictData")
    @Log(title = "更新字典数据", businessType = BusinessType.UPDATE, operatorType = OperatorType.OTHER)
    public AjaxResult updateDictData(@Validated DictDataDto DictDataDto) {
        DictDataDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.dictDataService.update(DictDataDto));
    }

    /*** 根据 ID 查询一个字典信息 */
    @GetMapping("getOne/{dictId}")
    public AjaxResult getDictData(@PathVariable @Validated @NotNull(message = "字典ID不能为空") Long dictId) {
        return AjaxResult.success(this.dictDataService.selectDictDataById(dictId));
    }

    /*** 删除 */
    @DeleteMapping("deleteDictDataByIds/{dictIds}")
    @Log(title = "删除字典数据", businessType = BusinessType.DELETE, operatorType = OperatorType.OTHER)
    public AjaxResult updateDictData(@PathVariable @Validated @NotEmpty(message = "要删除的 ID 不 能为空") Long[] dictIds) {
        return AjaxResult.toAjax(this.dictDataService.deleteDictDataByIds(dictIds));
    }

    /*** 查询所有可用的字典类型 */
    @GetMapping("getDataByType/{dictType}")
    public AjaxResult getDataByType(@PathVariable @Validated @NotEmpty(message = "字典类型不能为 空") String dictType) {
        return AjaxResult.success(this.dictDataService.selectDictDataByDictType(dictType));
    }
}
